package cn.hu.basic.controller;

import cn.hu.basic.util.AjaxResult;
import cn.hu.basic.util.FastdfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fast_dfs")
public class UploadController {

    @PostMapping
    public AjaxResult upload(@RequestPart MultipartFile file) throws IOException {
        //得到字节数组
        byte[] bytes = file.getBytes();
        //得到文件扩展名
        String fileName = file.getOriginalFilename();
        //截取文件扩展名
        String extName = fileName.substring(fileName.indexOf(".")+1);
        //上传
        String upload = FastdfsUtil.upload(bytes, extName);

        return AjaxResult.getInstance().setResultObject(upload);
    }

    @GetMapping
    public AjaxResult delete(@RequestParam String path){
        System.out.println(path);
        String pathTmp = path.substring(1);// group1/M00/03/3F/CgAIC2OdchKAQahQAACB2HiLDt4837.jpg
        String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
        String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// M00/03/3F/CgAIC2OdchKAQahQAACB2HiLDt4837.jpg
        FastdfsUtil.delete(groupName,remotePath);
        return AjaxResult.getInstance();
    }
}
